Ext.onReady(function() {
	
	//显示提示信息
	Ext.QuickTips.init();
	
	//统一设置提示风格，为side
	Ext.form.Field.prototype.msgTarget='side';	
	
	//片路径：一张1×1的透明图片，Ext使用它来和CSS一起创建内嵌的图标。 
	Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//数据源
 	var myTroubleTicketStore = new Ext.data.JsonStore({
			url: '../personnelRequirement/list.action',
			fields: ['_id', 'stationName', 'depId','depName','createEmpId','createEmpName', 'recruitNum', 'recruitReason','createTime','positionTime','status'],
			root: 'data', 
			totalProperty: 'total' 
		});
	myTroubleTicketStore.load({
		params: {
			start: 0,
			limit: 10
		}
	});	
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//列模型
	var colModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header: '序号', width: 35}), //加入序号
		selModel, //加入checkbox选择模型
		{header: '岗位名称', width: 100, dataIndex: 'stationName', sortable: true},
		{header: '部门名称', width: 100, dataIndex: 'depName', sortable: true},
		{header: '招聘人数', width: 100, dataIndex: 'recruitNum', sortable: true},
		{header: '申请人', width: 100, dataIndex: 'createEmpName', sortable: true},
		{header: '招聘原因', width: 100, dataIndex: 'recruitReason', sortable: true},
		{header: '到岗时间', width: 100, dataIndex: 'positionTime', sortable: true},
		{header: '创建时间', width: 100, dataIndex: 'createTime', sortable: true},
		{header: '状态', width: 100, dataIndex: 'status', sortable: true}
	]);
	colModel.defaultWidth  = 100;
	
	var myTroubleTicketGrid = new Ext.grid.GridPanel({
		title: '我的用人申请表',
		height: 450,
		autoWidth: true, 
		viewConfig: new Ext.grid.GridView({
			autoFill: true  
		}),
		stripeRows: true, 
		sm: selModel, 
		frame: true, 
		applyTo: 'personnelRequirement_div',
		store: myTroubleTicketStore,
		cm: colModel, //列模型
		tbar: new Ext.Toolbar([
			{text: '添加', icon: '../images/add.gif', handler: showAdd},
			{text: '修改', icon: '../images/edit.gif', handler: showUpdate},
			{text: '删除', icon: '../images/delete.gif', handler: doDel},
			{text: '申请', icon: '../images/dispatch.gif', handler: doDispatch},
			{text: '审批历史', icon: '../images/viewHistory.gif', handler: viewHistory_lo}
		]),
		bbar: new Ext.PagingToolbar({
			store: myTroubleTicketStore, 
			pageSize: 10,
			displayInfo: true,
			plugins: new Ext.ux.ProgressBarPager()
		})
	});
		var workexpdata=[[1,"不限"],[2,"1年以下"],[3,"1~2年"],[4,"3~5年"],[4,"6~7年"],[5,"8~10年"],[9,"10年以上"]];
	      var workexpmajor = new Ext.data.ArrayStore({
                data:workexpdata,
                fields:["id","name"]
             });
	      
	      var proreqdata=[[1,"市场营销"],[2,"会计"],[3,"人力资源管理"],[4,"金融"],[4,"国际贸易"],[5,"企业管理"],[9,"统计"]];
	      var proreqmajor = new Ext.data.ArrayStore({
                data:proreqdata,
                fields:["id","name"]
             });
	      
	       var agedata=[[1,"18~22"],[2,"22~30"],[3,"30~40"]];
	      var agemajor = new Ext.data.ArrayStore({
                data:agedata,
                fields:["id","name"]
             });
	var myTroubleTicketForm=new Ext.form.FormPanel({
       width : 890,
       buttonAlign :'right',
       autoHeight : true,
       frame : true,
       layout : "form", // 整个大的表单是form布局
       labelWidth :5,
       labelAlign : "right",
    
       items:[
       //第一行
   {
				id: '_id',
				name: 'personnelReq._id',
				xtype: 'hidden'
			},
       {
       collapsible :true,
       title:"招聘申请",
       items:[
       {layout:'column',
      frame :true,
    
     items:[
       {
    	
        columnWidth :0.5,
       layout:'form',
        
      
        labelWidth :90,
       items:[{
    	   
       
       
       xtype:'combo',
        
       fieldLabel: '申请&nbsp&nbsp招聘&nbsp部门',
       width: 205,
         displayField: 'depName',
        valueField: 'depId',
         triggerAction: 'all',
         allowBlank:false,
         blankText: '请选择部门',
         editable: false,
         mode: 'local', //该属性和以下方法为了兼容ie8
         emptyText: '请选择...',
         store: new Ext.data.JsonStore({ 
					url: '../department/findList.action',
					fields: ['depId', 'depName'],
					autoLoad: true //自动调用load方法
				}),
				id:'department.depId',
       hiddenName: 'personnelReq.department.depId'
        }]},{
        columnWidth :0.5,
       layout:'form',
       
       margin: '5 0 5 0',

        labelWidth :90,
       items:[{
     
        
        xtype : "textfield",
          fieldLabel : "招聘岗位名称",
          name: 'personnelReq.stationName',
          id:'stationName',
        width : 205,
        allowBlank:false,
        blankText: '请填写岗位名称'
       }]}]},

       //第二行
{layout:'column',

frame :true,
///
columnWidth :0.5,

items:[{
        columnWidth :0.5,
       layout:'form',
       labelWidth :90,
       items:[{
        
          margin: '10 20 5 7',
          xtype: 'numberfield',     
          fieldLabel: "人&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp数",     
         name: 'personnelReq.recruitNum',
         id:'recruitNum',
              emptyText: "--请选择--",  
              triggerAction: "all",//请设置为"all",否则默认为"query"的情况下，你选择某个值后，再此下拉时，只出现匹配选项，如果设为"all"的话，每次下拉均显示全部选项  
                mode: "local",  
                width: 205,
               typeAhead: true, //延时查询  
                typeAheadDelay:3000,  
                editable: true,//可以编辑
                 allowBlank:false,
                 minValue : 0,//可输入的最小值
                 blankText: '请输入人数'
                  }]}
                ,{
      columnWidth :0.5,
       layout:'form',
         
     
         labelWidth :90,
        
       items:[{
       
      format : 'Y-m-d',
      readOnly : true ,
        xtype : "datefield",
              fieldLabel : "需到 岗 时  间",
              width : 205,
              id:'time',
      name:'personnelReq.positionTime',
      id:'positionTime',
       allowBlank:false,
               
                 blankText: '请选择到岗时间'
       }] }
               ]
               ///ting
       },
       {
       //a1
     
       layout:'column',
      frame :true,
       //
       items:[
       {//b1
       layout:'form',
         columnWidth :0.5,
       
        
         labelWidth :90,
       items:[{
       width: 290,
        xtype : "textarea",
       
        
          name: 'personnelReq.recruitReason',
        id:'recruitReason',
               fieldLabel : '<div style="margin-top:25px">招 &nbsp聘&nbsp&nbsp &nbsp&nbsp原&nbsp 因:</div>',
              labelSeparator :'',
                 allowBlank:false,
               
                 blankText: '请输入招聘原因'
           }] 
       }//b1
       ,{ //b2
       layout:'form',
    
       labelWidth :90,
       columnWidth :0.5,
       
       items:[{
        xtype:"textarea",
             name: 'personnelReq.jobExplain',
             id:'jobExplain',
              fieldLabel : '<div style="margin-top:25px">说&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp明:</div>',
              labelSeparator :'',
               width : 290
             
             }]
       }//b2
       ]},
       { 
    frame :true,
       layout:'form',
      
       items:[{
        layout:'form',
     
       labelWidth :90,
       columnWidth :0.5,
       items:[{
       xtype:'textarea',
     id:'jobDescription',
       name: 'personnelReq.jobDescription',
      fieldLabel : '<div style="margin-top:25px">职&nbsp&nbsp位&nbsp&nbsp&nbsp&nbsp&nbsp描&nbsp&nbsp述:</div>',
              labelSeparator :'',
                 allowBlank:false,
               
                 blankText: '请输入职位描述',
       width:725
       }]
       }]
       }]
       }
       
        //第二行结束
       ,
       //第三行
       {collapsible :true,
       
        title:'任职条件',
       
       items:[{ 
     frame :true,
       layout:'column',
      
       items:[
       {layout:'form',
        style : 'border:0px solid black; border-top:0;',
       
       columnWidth:0.5,
       
       labelWidth :90,
       items:[
       { 
       
       xtype:'combo',
       fieldLabel: '专&nbsp业 &nbsp&nbsp&nbsp&nbsp要 &nbsp求',
        store: proreqmajor,
        width:205,
         displayField: 'name',
        valueField: 'id',
        hiddenName: 'personnelReq.proReq',
        id:'proReq',
         triggerAction: 'all',
         allowBlank:true,
        editable: false,
         mode: 'local', //该属性和以下方法为了兼容ie8
        emptyText: '请选择...'
        }]},{ 
       
        layout:'form',
        columnWidth:0.5,
       
        labelWidth :90, 
        items:[{
       xtype:'combo',
       fieldLabel: '工 作 经 验',
        store: workexpmajor,
        width: 205,
        displayField: 'name',
        valueField: 'id',
        hiddenName: 'personnelReq.workExp',
        id:'workExp',
         triggerAction: 'all',
         allowBlank:true,
        editable: false,
         mode: 'local', //该属性和以下方法为了兼容ie8
        emptyText: '请选择...'
        }]}]
             
       
       }, 
       { 
      frame :true,
       layout:'column',
    
       items:[
       { 
        layout:'form',
        columnWidth:0.5,
        labelWidth :90, 
        items:[{frame : false,
        xtype:'combo',
       fieldLabel: ' 年 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &nbsp&nbsp&nbsp&nbsp&nbsp 龄',
        store: agemajor,
        width: 205,
     
        displayField: 'name',
        valueField: 'id',
         triggerAction: 'all',
         hiddenName: 'personnelReq.age',
         id:'age',
         allowBlank:true,
        editable: false,
         mode: 'local', //该属性和以下方法为了兼容ie8
        emptyText: '请选择...'
        }]}, {
       
        
        columnWidth:0.5,
         labelWidth :90,
         layout:'form',   
         items:[{ 
         width:205,
 
         xtype:'radiogroup',
         fieldLabel:' 性&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp别',
                 items: [{ name: 'personnelReq.sex',id:'sex', inputValue: '0', boxLabel: '男' }, { id:'sex',name: 'personnelReq.sex',width:30, inputValue: '1',boxLabel: '女'},{ id:'sex',name: 'personnelReq.sex', inputValue: '2',boxLabel: '不限', checked: true }]
                  
                  }]}]
       },{
       layout:'column',
    
      frame :true,
       items:[
       {
        
        columnWidth:0.5,
         labelWidth :90,
         layout:'form',
         items:[{
       xtype:"textarea",
               name: 'personnelReq.quality',
               id:'quality',
               fieldLabel : '<div style="margin-top:10px">能力&nbsp&nbsp&nbsp&nbsp/&nbsp&nbsp&nbsp素质:</div>',
              labelSeparator :'',
              width :290,
             height:40
              }]
              },	
       {
        
        
        columnWidth:0.5,
         labelWidth :90,
         layout:'form',
         items:[{
       xtype:"textarea",
             frame : true,
              fieldLabel : '<div style="margin-top:10px">技能&nbsp&nbsp&nbsp&nbsp要求:</div>',
              labelSeparator :'',
              width : 290,
              height:40,
                name: 'personnelReq.tecRequest',
                id:'tecRequest'
            }]
              }
       
       ]// 
       }
       ]}

       //第三行结束
       
       ,{
       collapsible :true,
       layout:'form',
       title:'意见',
       collapsed:true,
       //
      
      id:'yijian',
       items:[
    	   {
      frame :true,
        layout:'column',
       items:[
       {labelWidth :90,
       layout:'form',
       columnWidth:0.5,
       items:[{
       name: 'personnelReq.depMgrOpinion',
       id:'depMgrOpinion',
       xtype:"textarea",
       fieldLabel : '<div style="margin-top:10px">部门&nbsp经理&nbsp&nbsp意见:</div>',
       labelSeparator :'',
       width:290,
        height:40
             }]},
             {
            	
        labelWidth :90,
       layout:'form',
       columnWidth:0.5, 
      
        items:[{
         xtype:"textarea",
              name: 'personnelReq.leaOption',
              id:'leaOption',
              fieldLabel : '<div style="margin-top:10px">上级&nbsp&nbsp&nbsp&nbsp主管:</div>',
              labelSeparator :'',
            
             width:290,
              height:40
             }]}
             
             ]
          },
          
       
       { frame :true,
        
       layout:'column',
       items:[
        {   
      
        labelWidth :90,
       layout:'form',
       columnWidth:0.5,
        
        items:[{
             xtype:"textarea",
           name: 'personnelReq.hrOption',
           id:'hrOption',
               fieldLabel : '<div style="margin-top:10px">人力资源部意见:</div>',
              labelSeparator :'',
           
             width :290,
              height:40
              
              }]
               
       },{
        
        labelWidth :90,
       layout:'form',
       columnWidth:0.5,
      
        items:[{
         xtype:"textarea",
          name: 'personnelReq.hrLeaOption',
          id:'hrLeaOption',
            fieldLabel : '<div style="margin-top:0px">人力资源部主管领导意见:</div>',
              labelSeparator :'',
          
             width :290,
              height:40
            
              }]
       }
       
       ]
       }
       ///
       
       ]
       //
       },
       //第四行结束
       {collapsed:true,
        collapsible :true,
       layout:'form',
       title:'签字',
       //
      
       items:[
        { frame :true,
       layout:'column', 
      
       items:[
        {
         labelWidth :90,
       layout:'form',
       columnWidth:0.5,
        
        items:[{
        	id:'viceGeMgrOption',
        	  name: 'personnelReq.viceGeMgrOption',
       xtype:"textarea",
              fieldLabel : '<div style="margin-top:10px">副&nbsp&nbsp总&nbsp&nbsp&nbsp&nbsp&nbsp意&nbsp&nbsp见:</div>',
              labelSeparator :'',
            
              width :290,
              height:40
             
              }]
       },{
       labelWidth :90,
       layout:'form',
       columnWidth:0.5,
        
        items:[{
         xtype:"textarea",
         id:'viceGeMgrSign',
        name: 'personnelReq.viceGeMgrSign',
              fieldLabel : '<div style="margin-top:10px">签&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp名:</div>',
              labelWidth :90,
              labelSeparator :'',
              width :290,
              height:40
            }]
              
       }
       
       ]
       },
     { 
       frame :true,
     
       layout:'column',
       items:[
        { 
         labelWidth :90,
       layout:'form',
       columnWidth:0.5,
        
        items:[{
        	id:'geMgrOption',
        	  name: 'personnelReq.geMgrOption',
       xtype:"textarea",
              fieldLabel : '<div style="margin-top:10px">总&nbsp经&nbsp理&nbsp&nbsp&nbsp意&nbsp见:</div>',
              labelSeparator :'',
             
             width :290,
              height:40
              
             }]
       },{
labelWidth :90,
       layout:'form',
       columnWidth:0.5,
       
        items:[{
         xtype:"textarea",
         id:'geMgrSign',
           name: 'personnelReq.geMgrSign',
              fieldLabel : '<div style="margin-top:10px">签&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp名:</div>',
              labelSeparator :'',
              width :290,
              height:40
             
              }]
       }
       
       ]
       }
       
       
       ]
       //
       },
       {title:'注：此表由用人部门填写，经人力资源部审核，报总经理批示后执行。'
       }
       //第五行结束
       ],
      
       buttons:[
			{text: '保存', icon: '../images/save.gif', handler: doSave},
			{text: '关闭', icon: '../images/close.gif', handler: doClose}]
       });
	
	
	
	

	var win = new Ext.Window({
		width: 930,
		height: 800,
		y:0,
		x:0,
		contrain :true,
		frame : true,
		//autoload
	items: [myTroubleTicketForm],
		resizable: false,
         closable: true,
         autoScroll : true,
         draggable :true,//拖动
        modal:true,
         maximizable: true,
         minimizable: true,
         
		closeAction: 'hide' //不销毁form对象，只是隐藏
	});

	function showAdd() {
		myTroubleTicketForm.getForm().reset();
		win.show();
	}
	
	function showUpdate() {
	var records = myTroubleTicketGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择需要修改的招聘申请！');	
		}else if (records.length > 1) {
			Ext.Msg.alert('提示', '一次只能修改一个招聘申请！');
		}else {
			win.setTitle('修改招聘申请信息');
			win.show();
			//将用户代码输入域修改为只读
			
			Ext.getCmp('department.depId').focus(true, 500);
			myTroubleTicketForm.getForm().load({
				url: '../personnelRequirement/edit.action',
				method: 'GET',
				params: {'personnelReq._id': records[0].get('_id')},
				waitTitle: '提示',
				waitMsg: '正在装载招聘申请数据，请稍后...',
				failure: function() {
					Ext.Msg.alert('提示', '装载部门数据失败！');
				}
			});
		}
	}
	
	function doSave() {
     
		myTroubleTicketForm.getForm().submit({
			url: '../personnelRequirement/add.action',
			method: 'POST',
			clientValidation: true,
			waitTitle: '提示',
			waitMsg: '正在保存数据，请稍后...',
			success: function() {
				myTroubleTicketStore.reload(); //重新加载数据
				Ext.Msg.alert('提示', '单据保存成功！', function() {
					win.hide();					
				});
			},
			failure: function() {
				Ext.Msg.alert('提示', '单据保存失败！');
			}
		});
	}
	
	function doClose() {
	
	}
	
	function doDel() {
	var records =myTroubleTicketGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择需要删除的招聘申请！');
		}else {
			Ext.Msg.confirm('提示', '确认删除？', function(data) {
				if (data == 'yes') {
					var depIds = [];	
					for (var i=0; i<records.length; i++) {
						depIds.push(records[i].get('_id'));
					}
					Ext.Msg.show({
						title: '提示',
						msg: '正在删除招聘申请，请稍后...'
					});
					Ext.Ajax.request({
						url: '../personnelRequirement/del.action',
						method: 'GET',
						params: {'_ids':depIds},
						callback: function(options, success, response ) {
							if (success) {
								myTroubleTicketStore.reload(); //重新装载
								Ext.Msg.alert('提示', '部门删除成功！');
							}else {
								Ext.Msg.alert('提示', '部门删除失败！');
							}
						}
					});
				}
			});
		}
	}
	
	function doDispatch() {
		var records = myTroubleTicketGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择单据！');
		}else {
			Ext.Msg.confirm('提示', '确认申请？', function(data) {
				if (data == 'yes') {
					var troubleTicketIds = [];	
					for (var i=0; i<records.length; i++) {
						troubleTicketIds.push(records[i].get('_id'));
					}
					Ext.Msg.show({
						title: '提示',
						msg: '正在申请，请稍后...'
					});
					Ext.Ajax.request({
						url: '../personnelRequirement/dispatch.action',
						method: 'GET',
						params: {'_ids':troubleTicketIds},
						callback: function(options, success, response ) {
							if (success) {
								myTroubleTicketStore.reload(); //重新装载
								Ext.Msg.alert('提示', '申请成功！');
							}else {
								Ext.Msg.alert('提示', '申请失败！');
							}
						}
					});
				}
			});
		}
	}
	
	function viewHistory_lo() {
		var records = myTroubleTicketGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择单据！');	
		}else if (records.length > 1) {
			Ext.Msg.alert('提示', '一次只能选择一个单据！');
		}else {
			var _id = records[0].get('_id');
			viewHistory(_id);
		}
	}
});